package LinkedListPackage2;

import LinkedListPackage.ListNode;

import java.util.List;

/**
 * @author Lzm
 * @version 1.0
 */
public class MyLinkedList {
  ListNode dummyHead;
  int size;
  public MyLinkedList() {
    dummyHead = new ListNode();
    size = 0;
  }

  public int get(int index) {
    if (index < 0 || index >= size){
      return -1;
    }
    ListNode cur = dummyHead;
    for (int i = 0 ; i < index ; i++){
      cur = cur.next;
    }
    return cur.val;
  }

  public void addAtHead(int val) {
    ListNode node = new ListNode(val);
    node.next = dummyHead.next;
    dummyHead.next = node;
    size++;
  }

  public void addAtTail(int val) {
    ListNode cur = dummyHead;
    while(cur.next != null){
      cur = cur.next;
    }
    ListNode node = new ListNode(val);
    cur.next = node;
    size++;
  }

  public void addAtIndex(int index, int val) {
    if (index < 0 || index >= size){
      return;
    }
    ListNode cur = dummyHead;
    for (int i = 0; i < index; i++) {
      cur = cur.next;
    }
    ListNode node = new ListNode(val);
    node.next = cur.next;
    cur.next = node;
    size++;
  }

  public void deleteAtIndex(int index) {
    if (index < 0 || index >= size){
      return;
    }
    ListNode cur = dummyHead;
    for (int i = 0; i < index; i++) {
      cur = cur.next;
    }
    cur = cur.next.next;
    size--;
  }
}
